package org.wmine.eventhandlers;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Enumeration;

import org.wmine.core.XmlConvert;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.wmine.core.DatabaseManager;

public class TableSelectionDialogBox extends JFrame implements ListSelectionListener,ActionListener
{
	JLabel lblTable,lblColumn,lblAttr;
	JList lstTable,lstColumn,lstAttr;
	JScrollPane scrTable,scrColumn,scrAttr;
	JButton btnLeft,btnRight,btnConvert;
	DefaultListModel defaultlistmodel1 , defaultlistmodel2;

	DatabaseManager dm=DatabaseManager.getInstance();
	
	public TableSelectionDialogBox()
	{
		Container c= getContentPane();
		setLayout(null);
		setBounds(200,150,600,500);
		
		lblTable=new JLabel("Tables");
		lblColumn=new JLabel("Column Names");
		lblAttr=new JLabel("Selected Attributes");
		
		lblTable.setBounds(10,10,150,50);
		lblColumn.setBounds(200,10,150,50);
		lblAttr.setBounds(420,10,200,50);
	
		
		lstTable=new JList();
		lstTable.setVisibleRowCount(10);
		scrTable=new JScrollPane(lstTable);
		scrTable.setBounds(10,80,150,250);
		
		lstTable.setListData(dm.getTableArray());
		lstTable.addListSelectionListener(this);
		
		defaultlistmodel1 = new DefaultListModel();
		lstColumn = new JList(defaultlistmodel1);
		
	
		/*lstColumn=new JList();
		lstColumn.setVisibleRowCount(10);*/
		scrColumn=new JScrollPane(lstColumn);
		scrColumn.setBounds(200,80,150,250);
		
		defaultlistmodel2 = new DefaultListModel();
		lstAttr = new JList(defaultlistmodel2);
		
		/*lstAttr=new JList();
		lstAttr.setVisibleRowCount(10);*/
		scrAttr=new JScrollPane(lstAttr);
		scrAttr.setBounds(420,80,150,250);
		
		
		
		btnRight=new JButton(">>");
		btnLeft=new JButton("<<");
		btnConvert=new JButton("Convet To Xml File");
		
		btnRight.setBounds(360,120,50,25);
		btnLeft.setBounds(360,250,50,25);
		btnConvert.setBounds(180,360,200,30);
		
		btnRight.addActionListener(this);
		btnLeft.addActionListener(this);
		btnConvert.addActionListener(this);
		
		
	
		c.add(lblTable);
		c.add(lblColumn);
		c.add(lblAttr);
		
		//c.add(lstColumn);
		c.add(scrTable);
		//c.add(lstAttr);
		c.add(scrColumn);
		c.add(scrAttr);
		
		c.add(btnLeft);
		c.add(btnRight);
		c.add(btnConvert);
		setVisible(true);
	}
	

	
	public void valueChanged(ListSelectionEvent e)
	{
		
		String tablename=lstTable.getSelectedValue().toString();
		JList lst=(JList)e.getSource();
		if(lst==lstTable)
		{
			System.out.println(tablename);
			defaultlistmodel1.clear();
			
//			dm.fillTableInformation(tablename);
			Iterator<String> columenIterator = dm.getTableInformation(tablename);
			
			//lstColumn.setListData(dm.column);
//			System.out.println("column size: "+dm.column.size());
			while(columenIterator.hasNext()) {
				String columnName = columenIterator.next();
				System.out.println(columnName);
				defaultlistmodel1.addElement(columnName);
			}
//			for(int i=0;i<dm.column.size();i++)
		
		}

	}
	public void actionPerformed(ActionEvent ae)
	{
		JButton btn=(JButton)ae.getSource();
		/*
		Vector leftListVector			=	new Vector();
		Vector rightListVector			=	new Vector();
	*/
		if(btn==btnRight)
		{
			
			//Object o=lstColumn.getSelectedValue();
			/*int index=lstColumn.getSelectedIndex();
			System.out.println(index);
		
			
			
			defaultlistmodel2.addElement(lstColumn.getSelectedValue());
			System.out.print(defaultlistmodel1.getSize());
			//if(defaultlistmodel1.getSize() > 0)
				defaultlistmodel1.remove(index);*/
			
			//defaultlistmodel1.r
			
			Object e[] = lstColumn.getSelectedValues();
			for(int i=0; i<e.length;i++)
			{
				if(defaultlistmodel2.indexOf(e[i])==-1)
				{
					defaultlistmodel2.addElement(e[i]);
				}
				defaultlistmodel1.remove(defaultlistmodel1.indexOf(e[i]));
			}
			
		}
		else if(btn==btnLeft)
		{
			Object e[] = lstAttr.getSelectedValues();
			for(int i=0; i<e.length;i++)
			{
                if(defaultlistmodel1.indexOf(e[i])==-1)
				{
                    defaultlistmodel1.addElement(e[i]);
				}
                defaultlistmodel2.remove(defaultlistmodel2.indexOf(e[i]));
			}
		}
		else
		 {
		 	if(btn==btnConvert)
		 	{
		  
		  		String tablename=lstTable.getSelectedValue().toString();
		  		//ArrayList colname = new ArrayList(); 	 
		  		StringBuffer colname = new StringBuffer();
				Enumeration e = defaultlistmodel2.elements();
					while(e.hasMoreElements())
					{
					 colname.append(e.nextElement());
					 //colname.add(e.nextElement());
					 colname.append(",");
					//colname.append()					
					}
				
				colname.deleteCharAt(colname.length()-1);
			
					System.out.print(colname);
					this.dispose();
		
				XmlConvert xm=new XmlConvert(tablename,colname);
				//System.out.println("aaaaaaaaa:"+tablename+" "+colname);
				//XmlConvert.setValues(tablename,colname);
				//xm.setDefaultCloseOperation(EXIT_ON_CLOSE);
		 	}
		 }
		
	}
	
}
